package lab.jsp;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lab.domain.Artist;
import lab.domain.ChangableHashSet;
import lab.domain.Genre;
import lab.domain.SoundRecordingStudio;
import lab.domain.Track;

/**
 *
 * @author Smike
 */
public class EditTrack extends AbstractJSP<Track> {

    public static final String ID = "id";
    public static final String NAME = "name";
    public static final String YEAR = "year";
    public static final String LENGTH = "length";
    public static final String STUDIO = "studio";
    public static final String ARTIST_COUNT = "artists";
    public static final String GENRES_COUNT = "genres";
    public static final String ARTIST = "artists[%d]";
    public static final String GENRE = "genres[%d]";
    
    Track track;

    @Override
    public void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // do nothing
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        track = new Track();
        String id = request.getParameter(ID);
        if (id != null) {
            track.setId(Integer.valueOf(id));
        } else {
            track.setId(-1);
        }
        super.doGet(request, response);
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        track = new Track();
        String id = request.getParameter(ID);
        if (id != null) {
            track.setId(Integer.valueOf(id));
        } else {
            throw new IllegalArgumentException();
        }
        try {
            if (request.getParameter("save") != null) {
                
                track.setName(request.getParameter(NAME));
                track.setYear(Integer.parseInt(request.getParameter(YEAR)));
                track.setLength(Integer.parseInt(request.getParameter(LENGTH)));
                
                SoundRecordingStudio studio = new SoundRecordingStudio();
                studio.setId(Integer.parseInt(request.getParameter(STUDIO)));
                
                track.setSoundRecordingStudio(studio);
                
                ChangableHashSet<Artist> artists = new ChangableHashSet<Artist>();
                int artistCount = Integer.parseInt(request.getParameter(ARTIST_COUNT));
                for (int i = 0; i < artistCount; i++) {
                    Artist artist = new Artist();
                    artist.setId(Integer.parseInt(request.getParameter(String.format(ARTIST, i))));
                    artists.add(artist);
                }
                track.setArtists(artists);
                
                ChangableHashSet<Genre> genres = new ChangableHashSet<Genre>();
                int genreCount = Integer.parseInt(request.getParameter(GENRES_COUNT));
                for (int i = 0; i < genreCount; i++) {
                    Genre genre = new Genre();
                    genre.setId(Integer.parseInt(request.getParameter(String.format(GENRE, i))));
                    genres.add(genre);
                }
                track.setGenres(genres);
                
                if (track.getId() >= 0) {
                    dao.updateTrack(track);
                    
                } else {
                    dao.insertTrack(track);
                }
            } else if (request.getParameter("delete") != null) {
                if (track.getId() >= 0) {
                    dao.deleteTrack(track);
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(EditTrack.class.getName()).log(Level.SEVERE, null, ex);
        }
        response.sendRedirect("tracks.jsp");
    }

    @Override
    protected Track getData() {
        Logger.getLogger(EditTrack.class.getName()).severe("id is " + track.getId());
        if (track.getId() >= 0) {
            try {
                return (Track) dao.findTrack(track.getId());
            } catch (Exception ex) {
                Logger.getLogger(EditTrack.class.getName()).log(Level.SEVERE, null, ex);
                throw new RuntimeException(ex);
            }
        } else {
            return track;
        }
    }
    
    public Collection<Artist> getArtists() {
        try {
            return dao.findArtists();
        } catch (Exception ex) {
            Logger.getLogger(EditTrack.class.getName()).log(Level.SEVERE, null, ex);
            return Collections.EMPTY_LIST;
        }
    }
    
    public Collection<Genre> getGenres() {
        try {
            return dao.findGenres();
        } catch (Exception ex) {
            Logger.getLogger(EditTrack.class.getName()).log(Level.SEVERE, null, ex);
            return Collections.EMPTY_LIST;
        }
    }
    
    public Collection<SoundRecordingStudio> getStudios() {
        try {
            return dao.findStudios();
        } catch (Exception ex) {
            Logger.getLogger(EditTrack.class.getName()).log(Level.SEVERE, null, ex);
            return Collections.EMPTY_LIST;
        }
    }
    
    
}
